package end.handler.timer;

import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import end.ser.InitialDataSER;

/**
 * 
 * StartupListener4 上午10:23:12 Copyright HEXUNz Inc. All right reserved. Email :
 * alldios@outlook.com
 * 
 */

public class StartupListener4InitialData implements ServletContextListener {
	private WebApplicationContext springContainner;

	public WebApplicationContext getSpringContainner() {
		return springContainner;
	}

	public void setSpringContainner(WebApplicationContext springContainner) {
		this.springContainner = springContainner;
	}

	@Override
	public void contextDestroyed(ServletContextEvent sce) {
		sce.getServletContext().log(" +　定时器销毁");
	}

	@Override
	public void contextInitialized(ServletContextEvent sce) {
		springContainner = WebApplicationContextUtils
				.getWebApplicationContext(sce.getServletContext());
		sce.getServletContext().log("　+　启动定时器");
		/**
		 * 构造一个ScheduledThreadPoolExecutor对象，并且设置它的容量为5个
		 */
		InitialDataSER initialDataSER = (InitialDataSER) springContainner
				.getBean("initialDataSER");
		ScheduledThreadPoolExecutor stpe = new ScheduledThreadPoolExecutor(5);
		TimerTaskForInitialMainData myTimerTaskForInitialMainData = new TimerTaskForInitialMainData(
				sce.getServletContext(), initialDataSER);
		TimerTaskForInitialPermissonData timerTaskForInitialPermissonData = new TimerTaskForInitialPermissonData(
				sce.getServletContext(), initialDataSER);
		/**
		 * @param2 启动应用后多长时间后开始执行一次
		 * @param3 每隔一段时间后开始执行一次
		 */
		stpe.scheduleAtFixedRate(myTimerTaskForInitialMainData, 10, 60 * 60,
				TimeUnit.SECONDS);
		stpe.scheduleAtFixedRate(timerTaskForInitialPermissonData, 10, 60 * 60,
				TimeUnit.SECONDS);
	}

}